#pragma once
#include <initializer_list>
#include <utility>
#include <cassert>
#include <algorithm>

using namespace std;

namespace wxvector
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector(){}

		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (auto& e : il)
			{
				push_back(e);
			}
		}

		template<class inputiterator>
		vector(inputiterator first, inputiterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		vector(const vector<T>& v)
		{
			vector<T> tem(v.begin(), v.end());
			swap(tem);
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		vector<int>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		vector(size_t n, T x = T())
		{
			resize(n, x);
		}

		vector(int n, T x = T())
		{
			resize(n, x);
		}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = nullptr;
				_finish = nullptr;
				_end_of_storage = nullptr;
			}
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity;
				reserve(newcapacity);
			}
			*_finish = x;
			_finish++;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		T& operator[](size_t n)
		{
			assert(n < size());
			return _start[n];
		}

		void clear()
		{
			_finish = _start;
		}

		void pop_back()
		{
			assert(size() > 0);
			_finish--;
		}

		iterator insert(iterator pos const T& val)
		{
			assert(_start <= pos && pos <= _finish);
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity;
				reserve(newcapacity);
				pos = _start + len;
			}
			iterator end = _finish;
			while (end > pos)
			{
				*end = *(end - 1);
				end--;
			}
			*pos = val;
			_finish++;
			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);
			assert(size());
			iterator end = pos;
			while (pos < _finish - 1)
			{
				*end = *(end + 1);
				end++;
			}
			_finish--;
			return pos;
		}

		const T& operator[](size_t n) const
		{
			assert(n < size());
			return _start[n];
		}

		void resize(size_t n, T val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*finish = val;
					finish++;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t len = size();
				T* newv = new T[n];
				if (_start)
				{
					for (size_t i = 0; i < size(); i++)
					{
						newv[i] = _start[i];
					}
					delete[] _start;
				}
				_start = newv;
				newv = nullptr;
				_finish = _start + len;
				_end_of_storage = _start + n;
			}
		}

		iterator find(const T& val)
		{
			for (size_t i = 0; i < size(); i++)
			{
				if (_start[i] == val)
				{
					return _start + i;
				}
			}
			return _finish;
		}

		bool empty() const
		{
			return _start == _finish;
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};
}
